$(function () {
    refreshTeacherList();
});

function refreshTeacherList() {
    $.ajax({
        url: "/api/teacher/list",
        method: "GET",
        success: function (data) {
            let html = "";
            data.forEach((element) => {
                html += "<tr>";
                html += "<td>" + element.id + "</td>";
                html += "<td>" + element.name + "</td>";
                html += "<td>" + element.sex + "</td>";
                html += "<td>" + element.age + "</td>";
                html += "<td>" + element.tno + "</td>";
                html += "<td>" + element.subject + "</td>";
                html += "<td> <a href='#' onclick='showTeacherDialog(" + element.id + ")'>编辑</a> &nbsp;&nbsp;<a href='#' onclick='deleteById(" + element.id + ")'>删除</a></td>";
                html += "</tr>";
            });
            $("#teacherTb").html(html);
        },
        error: function (err) {
            console.error("刷新教师列表失败：", err);
        }
    });
}

function showTeacherDialog(id) {
    let title = '新增教师';
    if (id) {
        title = '编辑教师';
        $("#fromId").css('display', 'block');
        $.ajax({
            url: "/api/teacher/" + id,
            method: "GET",
        }).done(result => {
            $.each(result, function (key, value) {
                var field = $('#teacherForm [name="' + key + '"]');
                if (field.is(':radio')) {
                    field.filter('[value="' + value + '"]').prop('checked', true);
                } else {
                    field.val(value);
                }
            });
        });
    } else {
        $("#teacherForm")[0].reset();
        $("#fromId").css('display', 'none');
    }

    layer.open({
        type: 1,
        title: title,
        area: ['500px', 'auto'],
        content: $('#teacherForm'),
    });
}

layui.form.on('submit', function () {
    event.preventDefault();
    commitTeacherDialog();
});

function commitTeacherDialog() {
    let id = $("#id").val();
    let formData = {};
    $("#teacherForm").serializeArray().forEach(item => {
        formData[item.name] = item.value;
    });

    if (id) {
        $.ajax({
            url: "/api/teacher/update",
            method: "PUT",
            contentType: "application/json",
            data: JSON.stringify(formData),
        }).done(() => {
            layer.closeAll();
            refreshTeacherList();
        }).fail(err => {
            console.error("更新失败：", err);
        });
    } else {
        $.ajax({
            url: "/api/teacher/add",
            method: "POST",
            contentType: "application/json",
            data: JSON.stringify(formData),
        }).done(() => {
            layer.closeAll();
            refreshTeacherList();
        }).fail(err => {
            console.error("新增失败：", err);
        });
    }
}

function deleteById(id) {
    layer.confirm('确定删除吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "/api/teacher/delete/" + id,
            method: "DELETE",
        }).done(() => {
            refreshTeacherList();
        }).fail(err => {
            console.error("删除失败：", err);
        });
        layer.closeAll();
    });
}